#!/bin/sh
Project_name=$1   
#Service_name=$2
TOMCAT_HOME=$Project_name
TOMCAT_START=$Project_name/bin/startup.sh
TOMCAT_STOP=$Project_name/bin/shutdown.sh
#JAVA_HOME=/usr/bin/java
JAVA_HOME=/opt/soft/jdk1.8.0_92/
Command=$2
my_Back="webapps_backup"
case "$Command" in
      start)
            cd $TOMCAT_HOME/bin
            sh startup.sh
            sleep 10
	    #/usr/bin/tail -n 500  $Project_name/logs/catalina.out 
            tompid=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'` 
            if ( [ -n "$tompid" ] );then
                 echo -e "$Project_name,启动成功...pid=$tompid" 
            else
                 echo -e "$Project_name,启动失败...."
           fi      
            ;;
       stop)
            pidlist=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`
            #echo "tomcat Id list :$pidlist"
            kill -9 $pidlist
            #echo "KILL $pidlist"
            sleep 5
            tompid=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`              
            if ( [ -n "$tompid" ] && [ $tompid ==  $pidlist ] );then
                 echo -e "$Project_name,停止失败...进程不存在..."
            else
                   #/usr/bin/tail -1000  $Project_name/logs/catalina.out 
                   echo -e "$Project_name,停止成功..tomcat-pid=$pidlist" 
            fi
   ;;
        status)
            tompid=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0" |awk '{print $2}'`
            if ( [ -n "$tompid" ] );then
                   echo -e "$Project_name,运行中......$tompid" 
            else
                   echo -e "$Project_name,停止中.....$tompid" 
            fi
            ;;
        restart)
                pidlist=`ps -ef|grep $Project_name|grep -v "grep"|grep -v "$0"|awk '{print $2}'`
                # echo "tomcat Id list :$pidlist"
                kill -9 $pidlist
                #echo "KILL $pidlist:"
                #echo "stop success"
                #echo "start tomcat"
                cd $Project_name
                 rm -rf work/*
                cd bin
                sleep 10
                ./startup.sh 
                 sleep 3
                 /usr/bin/tail -100  $Project_name/logs/catalina.out
                 echo -e "$Project_name,重启成功"
                 ;;
           backup) 
                 cd $Project_name
		echo "删除5天前的备份数据.."
                 find $Project_name/webapps_backup -mtime +5 -exec rm -rf {} \;
                 before_num=`/bin/ls webapps_backup/ |wc -l`
                 #`/bin/find  -type d  -name "webapps_[0-9]*" -mtime +10 | xargs rm -fr` 
                 #echo $my_Back
                 if [ ! -d "$my_Back" ]; then
              　     `/bin/mkdir -p $my_Back`
                  fi
                  /bin/cp -frp webapps  $my_Back/webapps_$(/bin/date -d "today" +"%Y%m%d_%H%M")
                  after_num=`/bin/ls webapps_backup/ |wc -l`
                  if [ $after_num -eq $(($before_num+1)) ];then
                       echo -e "$Project_name/webapp 备份成功"
		       rm -rf $Project_name/webapps/*
			echo -e "删除webapps/*"
		       rm -rf $Project_name/*.war
			echo -e "删除war"
                   else 
                        echo -e "$Project_name,备份失败，请重新备份"     
                   fi
                   ;;
           rollback) 
                   file_name=`/bin/ls -ltc $Project_name/$my_Back|sed -n 2p|awk '{print $8}'`
                   #echo "$Project_name/$my_Back/$file_name"
                   #echo $file_name
                   /bin/cp -frp $Project_name/$my_Back/$file_name   $Project_name/$my_Back/webapps
                   /bin/cp -frp $Project_name/$my_Back/webapps $Project_name
                   rm -fr $Project_name/$my_Back/webapps
                   cd $Project_name
                   if ([ "`find  ./ -maxdepth 1  -type d -cmin -1|sed -n 1p`"  == './webapps' ]);then
                         echo -e "$Project_name回滚成功"
                   else 
                         echo -e "$Project_name回滚失败，请重新执行一次"        
                   fi
                  ;;
               *)
                    echo "Usage: $0 {start|stop|restart|status|backup|rollback}" 
                    exit 1
esac
            
   exit 0
